package com.itheima.service.system.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.DeptDao;
import com.itheima.domain.system.Dept;
import com.itheima.service.system.DeptService;
import com.itheima.utils.MysqlUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class DeptServiceImpl implements DeptService {

    @Override
    public void save(Dept dept) {
        SqlSession sqlSession =null;
        try {
            sqlSession = MysqlUtils.getSqlSession();
            DeptDao mapper = sqlSession.getMapper(DeptDao.class);
            String id= UUID.randomUUID().toString();
            dept.setId(id);
            mapper.save(dept);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void delete(Dept dept) {
        SqlSession sqlSession =null;
        try {
            sqlSession = MysqlUtils.getSqlSession();
            DeptDao mapper = sqlSession.getMapper(DeptDao.class);

            mapper.delete(dept);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void update(Dept dept) {
        SqlSession sqlSession =null;
        try {
            sqlSession = MysqlUtils.getSqlSession();
            DeptDao mapper = sqlSession.getMapper(DeptDao.class);
            mapper.update(dept);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public Dept findById(String id) {
        SqlSession sqlSession =null;
        try {
            sqlSession = MysqlUtils.getSqlSession();
            DeptDao mapper = sqlSession.getMapper(DeptDao.class);
            return mapper.findById(id);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            sqlSession.close();
        }

    }

    @Override
    public List<Dept> findAll() {
        SqlSession sqlSession =null;
        try {
            sqlSession = MysqlUtils.getSqlSession();
            DeptDao mapper = sqlSession.getMapper(DeptDao.class);
            return mapper.findAll();
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {

        SqlSession sqlSession =null;
        try {
            sqlSession = MysqlUtils.getSqlSession();
            DeptDao mapper = sqlSession.getMapper(DeptDao.class);
            PageHelper.startPage(page, size);
            List<Dept> all=mapper.findAll();
            PageInfo pageInfo=new PageInfo(all);
            return pageInfo;
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            sqlSession.close();
        }
    }
}
